//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.rpt;

import com.aidong.common.JobConsts;
import com.aidong.dao.wills.ShopDprDao;
import com.aidong.model.wills.DayDprModel;
import com.aidong.service.CashierReportService;
import com.aidong.service.DayDprService;
import com.aidong.service.DepositReportService;
import com.aidong.service.MonthDprService;
import com.aidong.service.TbSummaryService;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.web.controller.BaseController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"createRpt"})
public class CreateRptController extends BaseController {
    @Resource
    TbSummaryService tbSummaryService;
    @Resource
    CashierReportService cashierReportService;
    @Resource
    DayDprService dayDprService;
    @Resource
    MonthDprService monthDprService;
    @Resource
    DepositReportService depositReportService;
    @Resource
    ShopDprDao shopDprDao;

    public CreateRptController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("rpt/createRpt/list");
        return mv;
    }

    @RequestMapping({"_createtb"})
    @ResponseBody
    public String _createtb(@RequestParam(value = "tbReportDate",required = false) String tbReportDate) {
        if(this.chkNull(tbReportDate)) {
            return this.getResult(false, "请输入生成报表的日期！");
        } else {
            try {
                this.tbSummaryService.createTbSummary(tbReportDate);
            } catch (Exception var3) {
                this.log.error("生成TB总表失败", var3);
                return this.getResult(false, "生成TB总表失败");
            }

            return this.getResult(true, "生成TB总表完成");
        }
    }

    @RequestMapping({"_createcr"})
    @ResponseBody
    public String _createcr(@RequestParam(value = "crReportDate",required = false) String crReportDate) {
        if(this.chkNull(crReportDate)) {
            return this.getResult(false, "请输入生成报表的日期！");
        } else {
            try {
                this.cashierReportService.createCashierReport(crReportDate);
            } catch (Exception var3) {
                this.log.error("生成Cashier Report总表失败", var3);
                return this.getResult(false, "生成Cashier Report总表失败");
            }

            return this.getResult(true, "生成Cashier Report总表完成");
        }
    }

    @RequestMapping({"_createdaydpr"})
    @ResponseBody
    public String _createdaydpr(@RequestParam(value = "dprReportDate",required = false) String dprReportDate) {
        if(this.chkNull(dprReportDate)) {
            return this.getResult(false, "请输入生成报表的日期！");
        } else {
            try {
                this.dayDprService.createDayDpr(dprReportDate);
            } catch (Exception var3) {
                this.log.error("生成DPR日报表失败", var3);
                return this.getResult(false, "生成DPR日报表失败");
            }

            return this.getResult(true, "生成DPR日报表完成");
        }
    }

    @RequestMapping({"_createdeposit"})
    @ResponseBody
    public String _createdeposit(@RequestParam(value = "depositMonth",required = false) String depositMonth) {
        if(this.chkNull(depositMonth)) {
            return this.getResult(false, "请输入生成报表的月份！");
        } else {
            try {
                this.depositReportService.createDepositReport(DateUtil.getLastDay(DateUtil.formatDate(depositMonth + "-01")));
            } catch (Exception var3) {
                this.log.error("生成剩余定金统计报表失败", var3);
                return this.getResult(false, "生成剩余定金统计报表失败");
            }

            return this.getResult(true, "生成剩余定金统计报表完成");
        }
    }

    @RequestMapping({"_createmonthdpr"})
    @ResponseBody
    public String _createmonthdpr(@RequestParam(value = "dprmonth",required = false) String dprmonth) {
        if(this.chkNull(dprmonth)) {
            return this.getResult(false, "请输入生成报表的月份！");
        } else {
            try {
                this.monthDprService.createMonthDpr(DateUtil.getLastDay(DateUtil.formatDate(dprmonth + "-01")));
            } catch (Exception var3) {
                this.log.error("生成DPR月报表失败", var3);
                return this.getResult(false, "生成DPR月报表失败");
            }

            return this.getResult(true, "生成DPR月报表完成");
        }
    }

    @RequestMapping({"_createdayfromtodpr"})
    public ModelAndView _createdayfromtodpr(@RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(this.chkNull(dateFrom)) {
            return this.showAlert("请输入开始日期。");
        } else if(this.chkNull(dateTo)) {
            return this.showAlert("请输入结束日期。");
        } else if(this.chkNull(dateFrom)) {
            return this.showAlert("请输入开始日期。");
        } else if(DateUtil.getMonth(DateUtil.formatDate(dateFrom)) != DateUtil.getMonth(DateUtil.formatDate(dateTo))) {
            return this.showAlert("请确保开始日期和结束日期在同一个月份。");
        } else {
            try {
                HashMap e = new HashMap();
                e.put("dateFrom", dateFrom);
                e.put("dateTo", dateTo);
                Date tmpDateFrom = DateUtil.formatDate(dateFrom);
                Date tmpDateTo = DateUtil.formatDate(dateTo);
                HashMap data1 = new HashMap();
                ArrayList listAll = new ArrayList();
                List list = this.shopDprDao.getDprDataOfList(tmpDateFrom, tmpDateTo);
                listAll.addAll(list);
                data1.putAll(e);
                data1.put("list", listAll);
                List list2 = this.shopDprDao.getDprDataOfTotal(tmpDateFrom, tmpDateTo);
                ArrayList memlist = new ArrayList();
                ArrayList ptlist = new ArrayList();
                Iterator fileName = list2.iterator();

                while(fileName.hasNext()) {
                    DayDprModel fileChName = (DayDprModel)fileName.next();
                    if("1".equals(fileChName.getAreaType())) {
                        memlist.add(fileChName);
                    } else if("2".equals(fileChName.getAreaType())) {
                        ptlist.add(fileChName);
                    }
                }

                data1.put("memlist", memlist);
                data1.put("ptlist", ptlist);
                String fileName1 = dateFrom + dateTo + "_" + JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX;
                String fileChName1 = dateFrom + dateTo + "_" + JobConsts.CH_NAME_DPR_INCOME_DAY + JobConsts.EXCEL_SUFFIX;
                ExportUtil.exportExcel(this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY), fileName1, this.getSetting(JobConsts.EXCEL_TEMPLATE_PATH_KEY), JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX, data1);
                this.download(fileName1, fileChName1, request, response);
                return null;
            } catch (Exception var16) {
                var16.printStackTrace();
                return null;
            }
        }
    }

    private String download(String fileName, String fileChName, HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream os = null;
        FileInputStream is = null;

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileChName.getBytes(), "iso-8859-1"));
            String e2 = this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY);
            is = new FileInputStream(new File(e2 + File.separator + fileName));
            os = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }

            os.close();
            is.close();
        } catch (FileNotFoundException var25) {
            var25.printStackTrace();
        } catch (IOException var26) {
            var26.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException var24) {
                    ;
                }
            }

            if(is != null) {
                try {
                    is.close();
                } catch (IOException var23) {
                    ;
                }
            }

        }

        return null;
    }
}
